package com.liquidhorse.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.liquidhorse.core.TLEvent;

public final class HTMLParser {
	
	private final static String PATTERN_CHUNK = 
		"<a name=\"event_(\\d+)\"></a>" // id
		+ "\\s*<div style=\"padding-left:5px;\">"
		+ "\\s*<table style=\"width:100%;\">"
		+ "\\s*<tr><td.*?>"
		+ "\\s*<span.*?>(.*?)</span><br><br>" // title
		+ "(.*?)" // content text
		+ "\\s*</td><td .*?>"
		+ "(.*?)" // status or countdown
		+ "\\s*</td></tr></table>"
		+ "\\s*</div>";

	private final static int ID = 1;
	private final static int TITLE = 2;
	private final static int DESCRIPTION = 3;
	private final static int STATUS = 4;
	
	public LinkedList<TLEvent> getEvents() {
		Matcher t = null;
		LinkedList<TLEvent> events = new LinkedList<TLEvent>();
		TLEvent e = null;
		
		Pattern p = Pattern.compile(PATTERN_CHUNK);
		Matcher matcher = p.matcher(this.getHTMLText());
		
		Pattern finished = Pattern.compile(".*(finished).*");
		Pattern running = Pattern.compile(".*(Running).*");
		Pattern days = Pattern.compile(".*?(\\d*)</strong> day.*");
		Pattern hours = Pattern.compile(".*?(\\d*)</strong> hour.*");
		Pattern minutes = Pattern.compile(".*?(\\d*)</strong> minute.*");
		Pattern kst = Pattern.compile(".*(\\d{2}):(\\d{2}) KST.*");
		Pattern cest = Pattern.compile(".*(\\d{2}):(\\d{2}) CEST.*");
		Pattern pdt = Pattern.compile(".*(\\d{2}):(\\d{2}) PDT.*");
		Pattern edt = Pattern.compile(".*(\\d{2}):(\\d{2}) EDT.*");
		
		while (matcher.find()) {
			e = new TLEvent();
			
			e.setId(Integer.parseInt(matcher.group(ID)));
			e.setTitle(matcher.group(TITLE));
			e.setDescription(matcher.group(DESCRIPTION).replaceAll("<a.*?>|</a>|<img.*?>|</img>", ""));
			
			t = finished.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.setFinished(true);
			}
			t = running.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.setRunning(true);
			}
			t = days.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.addDays(Integer.parseInt(t.group(1)));
			}
			t = hours.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.addHours(Integer.parseInt(t.group(1)));
			}
			t = minutes.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.addMinutes(Integer.parseInt(t.group(1)));
			}
			t = kst.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.setH_KST(Integer.parseInt(t.group(1)));
				e.setM_KST(Integer.parseInt(t.group(2)));
			}
			t = cest.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.setH_CEST(Integer.parseInt(t.group(1)));
				e.setM_CEST(Integer.parseInt(t.group(2)));
			}
			t = pdt.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.setH_PDT(Integer.parseInt(t.group(1)));
				e.setM_PDT(Integer.parseInt(t.group(2)));
			}
			t = edt.matcher(matcher.group(STATUS));
			if (t.find()) {
				e.setH_EDT(Integer.parseInt(t.group(1)));
				e.setM_EDT(Integer.parseInt(t.group(2)));
			}
			
			events.add(e);
		}
		
		return events;
	}
	
	private String getHTMLText() {
		StringBuilder text = new StringBuilder();
		BufferedReader br = null;
		HttpURLConnection con = null;
		
		try {
			URL url = new URL("http://www.teamliquid.net/calendar/2012/04/");
			con = (HttpURLConnection) url.openConnection();
			con.setReadTimeout(15000);
			con.setConnectTimeout(15000);
			con.setRequestMethod("GET");
			con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
			InputStreamReader isr = new InputStreamReader(con.getInputStream());
			br = new BufferedReader(isr);
			String line = new String();
			boolean endTag = false;
			while (!endTag && (line = br.readLine()) != null) {
				text.append(line);
				if (line.equals("</HTML>")) {
					endTag = true;
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				con.getInputStream().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return text.toString();
	}
}
